#include <math.h>

#include <iostream>

#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        if (nums.empty()) return 0;
        
        sort(nums.begin(), nums.end());
        
        int max_len = 1;
        int current_len = 1;
        
        for (int i = 1; i < nums.size(); ++i) {
            if (nums[i] == nums[i-1] + 1) {  // 连续递增
                current_len++;
            } else if (nums[i] != nums[i-1]) {  // 非重复元素时重置
                current_len = 1;
            }
            // 重复元素时保持当前长度不变
            
            max_len = max(max_len, current_len);
        }
        
        return max_len;
    }
};

// output: 4
void test01() {
    cout << "test01:" << endl;
    Solution obj;
    vector<int> input = {100,4,200,1,3,2};
    int re = obj.longestConsecutive(input);
    cout << re << endl;
}

// output: 9
void test02() {
    cout << "test02:" << endl;
    Solution obj;
    vector<int> input = {0,3,7,2,5,8,4,6,0,1};
    int re = obj.longestConsecutive(input);
    cout << re << endl;
}

// output: 3
void test03() {
    cout << "test03:" << endl;
    Solution obj;
    vector<int> input = {1,0,1,2};
    int re = obj.longestConsecutive(input);
    cout << re << endl;
}

// output: 2
void test04() {
    cout << "test04:" << endl;
    Solution obj;
    vector<int> input = {0, -1};
    int re = obj.longestConsecutive(input);
    cout << re << endl;
}

// output: 4
void test05() {
    cout << "test05:" << endl;
    Solution obj;
    vector<int> input = {1,0,-1,-2};
    int re = obj.longestConsecutive(input);
    cout << re << endl;
}

// [9,1,4,7,3,-1,0,5,8,-1,6]
// output: 7
void test06() {
    cout << "test06:" << endl;
    Solution obj;
    vector<int> input = {9,1,4,7,3,-1,0,5,8,-1,6};
    int re = obj.longestConsecutive(input);
    cout << re << endl;
}

// [0,1,2,4,8,5,6,7,9,3,55,88,77,99,999999999]
// output: 10
void test07() {
    cout << "test07:" << endl;
    Solution obj;
    vector<int> input = {0,1,2,4,8,5,6,7,9,3,55,88,77,99,999999999};
    int re = obj.longestConsecutive(input);
    cout << re << endl;
}

int main() {
    test01();
    test02();
    test03();
    test04();
    test05();
    test06();
    test07();
    return 0;
}
